﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.Web.Models
{
    public class ShoppingCartViewModel
    {
        IFoodService foodService;
        IRestaurantService restaurantService;
        IStaffService chefService;
        public ShoppingCartViewModel()
        {
            foodService = new FoodService();
            restaurantService = new RestaurantService();
            chefService = new StaffService();
        }

        public int Id { get; set; }
        public int Food_Id { get; set; }
        public int User_Id { get; set; }
        public int Chef_Id { get; set; }
        public int Restaurant_Id { get; set; }

        private Food food;
        public Food Food
        {
            get
            {
                if (food == null)
                {
                    food = foodService.Find(Food_Id);
                }

                return food;
            }
        }

        private Restaurant restaurant;
        public Restaurant Restaurant
        {
            get
            {
                if (restaurant == null)
                    restaurant = restaurantService.Find(this.Restaurant_Id);
                return restaurant;
            }
        }

        private Staff chef;
        public Staff Chef
        {
            get
            {
                if (chef == null)
                    chef = chefService.Find(this.Chef_Id);
                return chef;
            }
        }

        private short total = 1;
        public short Total
        {
            get { return total; }
            set { total = value; }
        }
        
        private ShoppingCart cart;
        public ShoppingCart ShoppingCart
        {
            get
            {
                if (cart == null)
                    cart = new ShoppingCart()
                    {
                        Id = this.Id,
                        Food_Id = this.Food_Id,
                        Restaurant_Id = this.Restaurant_Id,
                        User_Id = this.User_Id,
                        Chef_Id = this.Chef_Id,
                        Total = this.Total
                    };
                return cart;
            }
        }

        public static ShoppingCartViewModel FromShoppingCart(ShoppingCart cart)
        {
            var scvm = new ShoppingCartViewModel()
            {
                Id = cart.Id,
                Food_Id = cart.Food_Id,
                Restaurant_Id = cart.Restaurant_Id,
                User_Id = cart.User_Id,
                Chef_Id = cart.Chef_Id,
                Total = cart.Total
            };

            return scvm;
        }
    }
}